package com.x.attendance.assemble.control.jaxrs.attendance;

import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.JaxrsDescribe;
import com.x.base.core.project.annotation.JaxrsMethodDescribe;
import com.x.base.core.project.annotation.JaxrsParameterDescribe;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.ResponseFactory;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("attendance")
@JaxrsDescribe("考勤管理接口")
public class AttendanceAction extends StandardJaxrsAction {

    private static Logger logger = LoggerFactory.getLogger(AttendanceAction.class);

    @JaxrsMethodDescribe(value = "同步考勤数据", action = ActionSyncAttendanceData.class)
    @POST
    @Path("sync")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void syncAttendanceData(@Suspended final AsyncResponse asyncResponse,
                                   @Context HttpServletRequest request) {
        ActionResult<ActionSyncAttendanceData.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionSyncAttendanceData().execute(effectivePerson);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "获取考勤记录列表", action = ActionListAttendanceRecords.class)
    @POST
    @Path("records")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void listAttendanceRecords(@Suspended final AsyncResponse asyncResponse,
                                      @Context HttpServletRequest request,
                                      JsonElement jsonElement) {
        ActionResult<ActionListAttendanceRecords.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionListAttendanceRecords().execute(effectivePerson, jsonElement);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "获取考勤统计", action = ActionGetAttendanceStatistics.class)
    @GET
    @Path("statistics")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void getAttendanceStatistics(@Suspended final AsyncResponse asyncResponse,
                                        @Context HttpServletRequest request) {
        ActionResult<ActionGetAttendanceStatistics.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionGetAttendanceStatistics().execute(effectivePerson);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "推送数据到O2OA", action = ActionPushToO2OA.class)
    @POST
    @Path("push")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void pushToO2OA(@Suspended final AsyncResponse asyncResponse,
                           @Context HttpServletRequest request,
                           JsonElement jsonElement) {
        ActionResult<ActionPushToO2OA.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionPushToO2OA().execute(effectivePerson, jsonElement);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "从设备采集数据", action = ActionCollectDeviceData.class)
    @POST
    @Path("collect/{deviceId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void collectDeviceData(@Suspended final AsyncResponse asyncResponse,
                                  @Context HttpServletRequest request,
                                  @JaxrsParameterDescribe("设备ID") @PathParam("deviceId") String deviceId) {
        ActionResult<ActionCollectDeviceData.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionCollectDeviceData().execute(effectivePerson, deviceId);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "获取考勤记录详情", action = ActionGetAttendanceRecord.class)
    @GET
    @Path("record/{recordId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void getAttendanceRecord(@Suspended final AsyncResponse asyncResponse,
                                    @Context HttpServletRequest request,
                                    @JaxrsParameterDescribe("记录ID") @PathParam("recordId") String recordId) {
        ActionResult<ActionGetAttendanceRecord.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionGetAttendanceRecord().execute(effectivePerson, recordId);
        } catch (Exception e) {
            logger.error(e);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }
}

